/**
 *
 * Copyright 2015 RSVP Technologies Inc. All rights reserved.
 * Place.java
 *
 */
package com.gfinal.map.place;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gfinal.map.common.Constant;
import com.gfinal.map.util.HttpUtils;
import com.gfinal.map.util.Print;

/**
 * POI查询服务
 * 
 * @author Gao Yudong (gyd@nbicc.com)
 * @date 2015-2-25
 */
public class POISearch {
	public static final String TAG = POISearch.class.getName();
	private static final int PAGE_SIZE = 10;

	private String query = null;
	private int pageNum = 0;
	private int totlePage = -1;
	private String region = "全国";
	private float lat = 0;
	private float lng = 0;
	private int radius = 0;
	private boolean isSearchNearby = false;
	private boolean isFilter = false;
	/*
	 * 行业类型,取值:hotel:宾馆;cater:餐饮;life:生活娱乐
	 */
	private String industry_type = "life";
	/*
	 * 排序字段 (1)industry_type为hotel时，sort_name取值： default:默认； price:价格
	 * total_score:好评 level：星级 health_score：卫生 distance:距离排序，只有周边检索有效
	 * 
	 * (2)industry_type取值cater时，sort_name取值： default：默认 taste_rating：口味 price：价格
	 * overall_rating：好评 service_rating：服务 distance:距离排序，只有周边检索有效
	 * 
	 * (3)industry_type取值life时，sort_name取值： default：默认 price：价格
	 * overall_rating：好评 comment_num：服务 distance：距离排序，只有周边检索有
	 */
	private String sort_name = "default";
	/*
	 * 排序规则，取值如下：0：从高到低，1：从低到高
	 */
	private int sort_rule = 0;

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getTotlePage() {
		return totlePage;
	}

	public void setRegion(String region) {
		this.region = region;
	}

	public void setLat(float lat) {
		this.lat = lat;
	}

	public void setLng(float lng) {
		this.lng = lng;
	}

	public void setRadius(int radius) {
		this.radius = radius;
	}

	public void setSearchNearby(boolean isSearchNearby) {
		this.isSearchNearby = isSearchNearby;
	}

	public void setFilter(boolean isFilter) {
		this.isFilter = isFilter;
	}

	public void setIndustry_type(String industry_type) {
		this.industry_type = industry_type;
	}

	public void setSort_name(String sort_name) {
		this.sort_name = sort_name;
	}

	public void setSort_rule(int sort_rule) {
		this.sort_rule = sort_rule;
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
	private String createURI() {
		ArrayList params = new ArrayList();
		params.add(new BasicNameValuePair("ak", Constant.BAIDU_AK));
		params.add(new BasicNameValuePair("output", "json"));
		params.add(new BasicNameValuePair("scope", "2"));
		params.add(new BasicNameValuePair("page_size", String
				.valueOf(PAGE_SIZE)));
		params.add(new BasicNameValuePair("query", query));
		params.add(new BasicNameValuePair("page_num", String.valueOf(pageNum)));
		if (isSearchNearby) {
			params.add(new BasicNameValuePair("location", String.valueOf(lat)
					+ "," + String.valueOf(lng)));
			params.add(new BasicNameValuePair("radius", String.valueOf(radius)));
		} else {
			params.add(new BasicNameValuePair("region", region));
		}
		if (isFilter) {
			String filter = "industry_type:" + industry_type + "|sort_name:"
					+ sort_name + "|sort_rule:" + sort_rule;
			params.add(new BasicNameValuePair("filter", filter));
		}
		return Constant.PLACE_SEARCH_URL + "?"
				+ URLEncodedUtils.format(params, Constant.CHARSET);
	}

	public ArrayList<POIBean> execute() {
		ArrayList<POIBean> poiList = new ArrayList<POIBean>();
		String response = HttpUtils.doGet(createURI());
		JSONObject obj = JSON.parseObject(response);
		if (obj.getIntValue("status") == 0) {
			if (totlePage == -1) {
				int total = obj.getIntValue("total");
				totlePage = total / PAGE_SIZE
						+ ((total % PAGE_SIZE > 0) ? 1 : 0);
			}
			JSONArray poiArr = obj.getJSONArray("results");
			for (int i = 0; i < poiArr.size(); i++) {
				JSONObject poiObj = (JSONObject) poiArr.get(i);
				POIBean poi = JSON.parseObject(poiObj.toJSONString(),
						POIBean.class);
				poiList.add(poi);
			}
		}
		return poiList;
	}

	public static void main(String[] args) {
		POISearch search = new POISearch();
		search.setQuery("天一广场");
		search.setRegion("宁波");
		search.setPageNum(0);
		search.setSearchNearby(false);
		search.setLat(37.826215962191f);
		search.setLng(120.75393083871f);
		search.setRadius(2000);
		search.setFilter(false);
		ArrayList<POIBean> list = search.execute();
		Print.pln(TAG, list);
	}
}
